import com.mongodb.*;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaoji on 15/1/2.
 */
public class MongoDb {

    public static  String DBURL = "127.0.0.1:27017";

    public static Mongo collection = null;

    public static DB db = null;

    public MongoDb(String dbName) throws UnknownHostException {
        collection = new Mongo(DBURL);
        db = collection.getDB(dbName);
    }

    public void createCollection(String collectioname){
        DBObject dbObject = new BasicDBObject();
        db.createCollection(collectioname,dbObject);
    }

    public void insert(DBObject dbObject,String collectioName){
        DBCollection dbCollection = db.getCollection(collectioName);
        dbCollection.insert(dbObject);
    }

    public void insert(List<DBObject> dbObjects,String collectioName){
        DBCollection dbCollection = db.getCollection(collectioName);
        dbCollection.insert(dbObjects);
    }

    public static void main(String []args){
        //实例化
        try {
            MongoDb m = new MongoDb("zhaoji");
            //m.createCollection("bigdata");
            DBObject dbs = new BasicDBObject();
            dbs.put("name","zhaoji");
            List<String> books = new ArrayList<String>();
            books.add("AngularJS");
            books.add("cc");
            books.add("aa");
            dbs.put("books",books);

            DBObject dbs2 = new BasicDBObject();
            dbs2.put("name","zhaoji2222");
            List<String> books1 = new ArrayList<String>();
            books.add("Sentouch");
            books.add("cc1");
            books.add("cc2");
            dbs2.put("books",books1);
            List<DBObject> dbss = new ArrayList<DBObject>();
            dbss.add(dbs);
            dbss.add(dbs2);
            m.insert(dbs,"bigdata");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

}
